package com.hanhanxiaochong.zhongxiangchuanmei.view.home.act;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.SystemGiveBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.SystemGiveItem;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseLayout;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.adapter.SystemGiveAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/4/30.
 * Desc   : 我的礼包--系统赠送
 */

public class SystemGiveLayout extends BaseLayout {

    @BindView(R.id.recyclerview_sys)
    RecyclerView recyclerviewSys;

    private SystemGiveAdapter systemGiveAdapter;

    public SystemGiveLayout(Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_system_give;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void init() {
        String token = SharedPreferencesUtil.getsInstances(mContext).getLoginInfo().getToken();
        recyclerviewSys.setLayoutManager(new LinearLayoutManager(mContext));
        RetrofitSource.createApi(HttpPostService.class).disc(token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<SystemGiveBean>>(this) {
                    @Override
                    public void onSuccess(ResponseBase<SystemGiveBean> response) {
                        SystemGiveBean data = response.getData();
                        List<SystemGiveItem> list = new ArrayList<>();
                        List<SystemGiveItem> projectList = data.getPj();
                        List<SystemGiveItem> productList = data.getPd();
                        List<SystemGiveItem> systemGiveList = data.getPf();
                        for (SystemGiveItem item : projectList)
                            item.setType("project");
                        for (SystemGiveItem item : productList)
                            item.setType("product");
                        for (SystemGiveItem item : systemGiveList)
                            item.setType("systemGive");
                        list.addAll(projectList);
                        list.addAll(productList);
                        list.addAll(systemGiveList);
                        systemGiveAdapter = new SystemGiveAdapter(mContext, list);
                        recyclerviewSys.setAdapter(systemGiveAdapter);
                    }
                });

    }
}
